package com.rjt.article.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.rjt.article.dto.ArticleVisitDto;
import com.rjt.article.model.ArticleNumModel;
import com.rjt.article.model.ArticleVisitModel;
import com.rjt.article.repository.ArticleNumRepository;
import com.rjt.article.repository.ArticleVisitRepository;
import com.rjt.article.service.ArticleVisitService;
import com.rjt.article.vo.ArticleVisitVo;
import com.rjt.common.model.Address;
import com.rjt.common.util.TextUtil;
@Service
public class ArticleVisitServiceImpl implements ArticleVisitService {
	
	@Autowired
	private ArticleVisitRepository articleVisitRepository;
	
	@Autowired
	private ArticleNumRepository articleNumRepository;
	
	
	
	@Override
	public void save(String rId, HttpServletRequest request) {
		// TODO Auto-generated method stub
		try {
			ArticleVisitModel articleVisitModel = new ArticleVisitModel();
			articleVisitModel.setrId(rId);
			articleVisitModel.setCreateDate(new Date());
			String remortIP = TextUtil.getRemortIP(request);
			System.out.println("类ArticleVisitServiceImpl获取的远端的ip"+remortIP);
			if(Objects.isNull(remortIP)) {
				return ;
			}
			articleVisitModel.setIp(remortIP);
			Address place = TextUtil.getPlace(remortIP);
			articleVisitModel.setRegion(place.getProvince());
			articleVisitModel.setCity(place.getCity());
			articleVisitRepository.save(articleVisitModel);
			ArticleNumModel articleNumModel = articleNumRepository.findOne(rId);
			articleNumModel.setReadNum(String.valueOf(Integer.parseInt(articleNumModel.getReadNum())+1));
			articleNumRepository.save(articleNumModel);
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("保存访客信息失败了,可能是重复的ip,错误代码"+e.getMessage());
		}
		
	}
	@Override
	public List<ArticleVisitVo> countByRIdAndArea(String rId) {
		// TODO Auto-generated method stub
		 List<ArticleVisitVo> countByArea =new ArrayList<>();
		 List<ArticleVisitDto> countByRIdAndArea = articleVisitRepository.countByRIdAndArea(rId);
		 countByRIdAndArea.forEach(dto->{
			 ArticleVisitVo articleVisitVo = new ArticleVisitVo();
			 articleVisitVo.setName(dto.getRegion()+dto.getCity());
			 articleVisitVo.setValue(dto.getValue());
			 countByArea.add(articleVisitVo);
		 });
		 return countByArea;
		
	}
	
	@Override
	public List<ArticleVisitVo> countByRIdAndDate(String rId) {
		// TODO Auto-generated method stub
		 List<ArticleVisitVo> countByDate =new ArrayList<>();
		 List<ArticleVisitDto> countByRIdAndDate = articleVisitRepository.countByRIdAndDate(rId);
		 countByRIdAndDate.forEach(dto->{
			 ArticleVisitVo articleVisitVo = new ArticleVisitVo();
			 articleVisitVo.setName(dto.getCreateDate());
			 articleVisitVo.setValue(dto.getValue());
			 countByDate.add(articleVisitVo);
		 });
		 return countByDate;
	}
	
   
}
